<?php

namespace App\Http\Controllers\Guard\Shop;

use App\Http\Controllers\Common\GuardController;
use App\Models\Shop\Goods\GoodsClassify;
use App\Models\Shop\Goods\GoodsSpec;
use App\Models\Shop\Goods\GoodsSpecValue;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class GoodsSpecController extends GuardController {

    public function lst(Request $request) {
        $conditions = $request->input();
        $records = (new GoodsSpec())->getLst($conditions)->with(['getSpecValue:id,name,spec_id','getClassify:id,name'])->get();
        foreach ($records as $record) {
            $record->spec_values = $record->getSpecValue->pluck('name')->implode('      |       ');
            $record->new = '';//为前端提供的一个添加属性字段，于后端来讲，这个字段没有意义
        }

        $conditions['pid'] = 0;
        $classifies = (new GoodsClassify())->getLst($conditions)->orderBy('id')->with('children')->get();
        $classifies->push(collect(['id'=>0,'name'=>'全部','children'=>[]]));
        $this->handleChildren($classifies);

        return responseSuccess($records->toArray(),0,100000,compact('classifies'));
    }

    public function add(Request $request) {
        $classify_id = $request->input('classify_id');
        $spec_name = $request->input('spec_name');
        $spec_id = $request->input('spec_id');
        $spec_values = $request->input('spec_values',[]);

        $this->validate($request, [
            'classify_id'=>'required',
            'spec_name'=>'required',
            'spec_id'=>'required',
            'spec_values'=>'required|array',
        ]);


        try{
            DB::beginTransaction();
            $spec = GoodsSpec::updateOrCreate(['id'=>$spec_id],['name'=>$spec_name,'classify_id'=>$classify_id]);

            //新增
            if($spec_id<=0){
                foreach ($spec_values as &$spec_value) {
                    $spec_value['spec_id'] = $spec->id;
                    $spec_value['created_at'] = date('Y-m-d H:i:s');
                    $spec_value['updated_at'] = date('Y-m-d H:i:s');
                }
                GoodsSpecValue::insert($spec_values);
            }else{
                foreach ($spec_values as $spec_value) {
                    if(!empty($spec_value['id'])){
                        GoodsSpecValue::where('id',$spec_value['id'])->update(['name'=>$spec_value['name'],'spec_id'=>$spec_value['spec_id']]);
                    }else{
                        if(!empty($spec_value['name'])){
                            GoodsSpecValue::create(['name'=>$spec_value['name'],'spec_id'=>$spec_id]);
                        }
                    }

                }
            }


            DB::commit();
            return responseSuccess();
        }catch(\Exception $e){
            DB::rollback();
            $tip = $e->getFile().':'.$e->getLine().':'.$e->getMessage();
            return responseFail($tip);
        }
    }

    public function detail(Request $request) {
        $ids = $request->input('id',0);
        $records = (new GoodsSpec())->getLst(compact('ids'))->with(['getClassify:id,name','getSpecValue:id,name,spec_id'])->get();
        return responseSuccess($records->toArray());
    }

    public function del(Request $request) {
        $id = $request->input('id',0);


        //TODO 检查当前spec_id有没有哪个商品绑定


        return $this->delRecords(GoodsSpec::class, $id);
    }
}
